import { httpClient, ApiResponse } from '../http/client';

export interface DatabaseInstance {
  id: string;
  name: string;
  engine: string;
  version: string;
  status: 'running' | 'stopped' | 'pending' | 'maintenance';
  region: string;
  instanceType: string;
  storage: number;
  connections: number;
  cpu: number;
  memory: number;
  createdAt: string;
}

export interface CreateDatabaseRequest {
  name: string;
  engine: string;
  version: string;
  instanceType: string;
  region: string;
  storage: number;
}

export interface DatabaseMetrics {
  timestamp: string;
  cpu: number;
  memory: number;
  connections: number;
  iops: number;
}

export interface SlowQuery {
  id: string;
  query: string;
  duration: number;
  database: string;
  timestamp: string;
  impact: 'high' | 'medium' | 'low';
}

export class DatabaseAPI {
  // Instance Management
  static async getDatabases(): Promise<ApiResponse<DatabaseInstance[]>> {
    return httpClient.get<DatabaseInstance[]>('/api/databases');
  }

  static async getDatabase(id: string): Promise<ApiResponse<DatabaseInstance>> {
    return httpClient.get<DatabaseInstance>(`/api/databases/${id}`);
  }

  static async createDatabase(data: CreateDatabaseRequest): Promise<ApiResponse<DatabaseInstance>> {
    return httpClient.post<DatabaseInstance>('/api/databases', data);
  }

  static async updateDatabase(id: string, data: Partial<DatabaseInstance>): Promise<ApiResponse<DatabaseInstance>> {
    return httpClient.patch<DatabaseInstance>(`/api/databases/${id}`, data);
  }

  static async deleteDatabase(id: string): Promise<ApiResponse<void>> {
    return httpClient.delete<void>(`/api/databases/${id}`);
  }

  static async startDatabase(id: string): Promise<ApiResponse<DatabaseInstance>> {
    return httpClient.post<DatabaseInstance>(`/api/databases/${id}/start`);
  }

  static async stopDatabase(id: string): Promise<ApiResponse<DatabaseInstance>> {
    return httpClient.post<DatabaseInstance>(`/api/databases/${id}/stop`);
  }

  static async restartDatabase(id: string): Promise<ApiResponse<DatabaseInstance>> {
    return httpClient.post<DatabaseInstance>(`/api/databases/${id}/restart`);
  }

  // Performance Monitoring
  static async getMetrics(id: string, timeRange?: string): Promise<ApiResponse<DatabaseMetrics[]>> {
    const params = timeRange ? `?timeRange=${timeRange}` : '';
    return httpClient.get<DatabaseMetrics[]>(`/api/databases/${id}/metrics${params}`);
  }

  static async getCurrentMetrics(id: string): Promise<ApiResponse<DatabaseMetrics>> {
    return httpClient.get<DatabaseMetrics>(`/api/databases/${id}/metrics/current`);
  }

  // Slow Queries
  static async getSlowQueries(id?: string, limit?: number): Promise<ApiResponse<SlowQuery[]>> {
    const params = new URLSearchParams();
    if (id) params.append('databaseId', id);
    if (limit) params.append('limit', limit.toString());
    
    const queryString = params.toString();
    return httpClient.get<SlowQuery[]>(`/api/databases/slow-queries${queryString ? `?${queryString}` : ''}`);
  }

  static async optimizeQuery(queryId: string): Promise<ApiResponse<{ suggestions: string[] }>> {
    return httpClient.post<{ suggestions: string[] }>(`/api/databases/slow-queries/${queryId}/optimize`);
  }

  static async explainQuery(queryId: string): Promise<ApiResponse<{ plan: any }>> {
    return httpClient.post<{ plan: any }>(`/api/databases/slow-queries/${queryId}/explain`);
  }

  static async killQuery(queryId: string): Promise<ApiResponse<void>> {
    return httpClient.post<void>(`/api/databases/slow-queries/${queryId}/kill`);
  }

  // Storage Management
  static async getStorageUsage(id?: string): Promise<ApiResponse<any>> {
    const params = id ? `?databaseId=${id}` : '';
    return httpClient.get<any>(`/api/databases/storage${params}`);
  }

  static async getStorageGrowth(id: string, days?: number): Promise<ApiResponse<any[]>> {
    const params = days ? `?days=${days}` : '';
    return httpClient.get<any[]>(`/api/databases/${id}/storage/growth${params}`);
  }
}

export default DatabaseAPI;